package solitaire.presentation ;

//-------------------------------------------------------------------------------------

//import solitaire.controle.* ;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.datatransfer.Transferable;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import solitaire.InterfacesControle.ICCarte;
import solitaire.InterfacesPresentation.IPCarte;
import solitaire.controle.CCarte;

//-------------------------------------------------------------------------------------

public class PCarte extends JPanel implements IPCarte{

	//protected CCarte controle ;
	protected JLabel face ;
	protected JLabel dos ;
	protected ImageIcon icone ;
	private ICCarte controleur;
	private DragSource ds;
	private DragGestureListener dgl;
	private DragSourceListener dsl;

	//public PCarte (final String chaine, final CCarte controle) {
	public PCarte (int valeur, int couleur) {
		String carte = convertToPCarte(valeur) + intToString(couleur);

		initDimensions ();
		//this.controle = controle ;
		setLayout (null) ;
		setBackground (Color.yellow) ;
		icone = new ImageIcon (ClassLoader.getSystemResource("cartesCSHD/" + carte + ".gif")) ;
		face = new JLabel (icone) ;
		add (face) ;
		face.setLocation (0, 0) ;
		face.setSize (largeur, hauteur) ;
		ImageIcon iconeDos = new ImageIcon (ClassLoader.getSystemResource("cartesCSHD/bluegreencrisscross.jpg")) ;
		dos = new JLabel (iconeDos) ;
		dos.setSize (face.getSize ()) ;
		add (dos) ;
		dos.setLocation (0, 0) ;
		dos.setVisible(false);
		setOpaque (false) ;
		setSize (face.getSize ()) ;
		setPreferredSize (getSize ()) ;
		setFaceVisible(false);
		
		ds = new DragSource();
		DnDListener dndlistener=DnDListener.getInstance();
		dgl=dndlistener;
		dsl=dndlistener;
		ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, dgl);
	}

	public void setFaceVisible (boolean faceVisible) {
		face.setVisible (faceVisible) ;
		dos.setVisible (!faceVisible) ;
	}

	public void paint (Graphics g) {
		super.paint (g) ;
		g.drawRect (0, 0, getWidth () - 1, getHeight () - 1) ;
	}

	@Override
	public ImageIcon getIcon() {
		return (icone) ;
	}

	public static int largeur ;
	public static int hauteur ;

	public static void initDimensions () {
		ImageIcon icone = new ImageIcon (ClassLoader.getSystemResource("cartesCSHD/1C.gif")) ;
		largeur = icone.getIconWidth () ;
		hauteur = icone.getIconHeight () ;
	}

	public static void main (String args []) {
		initDimensions () ;
		JFrame f = new JFrame ("Test PCarte") ;
		f.getContentPane ().add (new PCarte (1, 1)) ;
		f.addWindowListener (new WindowAdapter () {
			public void windowClosing (WindowEvent e) {
				System.exit (0) ;
			}
		}) ;
		f.pack () ;
		f.setVisible (true) ;
	}

	@Override
	public void repaindre() {
		super.repaint();

	}

	@Override
	public void setPosition(int x, int y) {
		super.setLocation(x, y);

	}

	@Override
	public void setControleur(CCarte cCarte) {
		controleur=cCarte;
	}

	@Override
	public JPanel getJPanel() {
		return this;
	}
	
	//couleur - comprise entre 1 et 4 (1 = diamond, 2 = spade, 3 = heart, 4 = club)
	public String intToString(int couleur){
		if(couleur == 1){
			return "D";
		} else if(couleur == 2){
			return "S";
		} else if(couleur == 3){
			return "H";
		} else {
			return "C";
		}
	}
	
	public String convertToPCarte(int valeur){
		if(valeur == 11){
			return "J";
		} else if (valeur == 12){
			return "Q";
		} else if (valeur == 13){
			return "K";
		} else {
			return valeur+"";
		}
	}

	@Override
	public ICCarte getControleur() {
		return controleur;
	}
	
	@Override
	public boolean debutDnD(DragGestureEvent dge, Transferable tasTemp) {
		
		if(face.isVisible()){
			ds.startDrag(dge, DragSource.DefaultMoveNoDrop, tasTemp, dsl);
			repaint();
			return true;
		}
		return false;
			
		
	}
}


//-------------------------------------------------------------------------------------
